package someTestExcemple.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadTest1 {
    //main方法由默认的主线程执行的
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //线程创建方式1
        Thread t1 = new MyThread(30,"循环");
        t1.start();
        //线程创建方式2
        //Thread t2 = new Thread(new MyRunnable1(7));
        Runnable myRunnable = new MyRunnable1(7);
        Thread t2 = new Thread(myRunnable);
        t2.start();
        t2.join(); //让调用当前这个方法的线程先执行完！再往下跑
        ////线程创建方式3
        Callable myCallable = new MyCallable1(8);
        //还需要把Callable对象包装FutureTask对象
        //未来任务对象的作用:1.本身继承了Runnable接口，
        // 2.线程执行完毕后可以获取线程的返回值(get方法)
        FutureTask<String> futureTask = new FutureTask<>(myCallable);
        Thread t3 = new Thread(futureTask);
        t3.start();
        //注意(上面这个线程还没跑完)这里的get方法会阻塞线程，直到线程执行完毕，才会返回线程的返回值
        //MyCallable1.test1();
        System.out.println(futureTask.get());


    }
    //Thread的常用方法
    public static void test1() throws InterruptedException {
        //获取当前线程
        Thread thread = Thread.currentThread();
        System.out.println(thread.getName());
        Thread.sleep(1000);
        thread.setName("main--1111-");
        System.out.println(thread.getName());
        thread.start();
        thread.join(); //让调用当前这个方法的线程先执行完！
    }
}
